package net.sansi.v3correctserver.service.show.impl;

import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.entity.image.RecCmmImage;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.screen.Rect;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.show.ShowService;
import net.sansi.v3correctserver.service.show.SoftwareShowService;
import net.sansi.v3correctserver.util.RGBUtil;
import net.sansi.v3correctserver.util.ShowUtil;
import org.springframework.stereotype.Service;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 */

@Service
@Slf4j
public class SoftwareShowServiceImpl implements SoftwareShowService {

    /**
     * 打屏
     *
     * @param area     区域
     * @param bright   亮度
     * @param colorIdx 颜色索引
     * @param span     打屏间隔
     */
    @Override
    public ShowService.ShowClose show(CorrectArea area, int bright, int colorIdx, int span, int ofx, int ofy) {
        CorrectCnf correctCnf = AppHelper.projectSpace().getCorrectCnf();
        Rect showArea = area.getShowArea();
        RecCmmImage showImg = new RecCmmImage(
                correctCnf.getScreenWidth(),
                correctCnf.getScreenHeight()
        );
        int l = showArea.getLeft(), r = showArea.getRight();
        int t = showArea.getTop(), b = showArea.getBottom();
        for (int x = l; x <= r; x++) {
            for (int y = t; y <= b; y++) {
                if ((x - ofx) % span == 0 && (y - ofy) % span == 0) {
                    showImg.setColor(x, y, RGBUtil.brightToPhotoColor(colorIdx, bright));
                }
            }
        }
        ShowUtil.show(showImg);
        return this::close;
    }

    @Override
    public void close() {
        ShowUtil.close();
    }
}
